{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "world_alcohol的类型：\n",
      " <class 'numpy.ndarray'>\n",
      "world_alcohol的详细信息：\n",
      " [['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value']\n",
      " ['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']\n",
      " ['1986' 'Americas' 'Uruguay' 'Other' '0.5']\n",
      " ...\n",
      " ['1987' 'Africa' 'Malawi' 'Other' '0.75']\n",
      " ['1989' 'Americas' 'Bahamas' 'Wine' '1.5']\n",
      " ['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]\n",
      "world_alcohol第2行第2列元素值：\n",
      " Uruguay\n"
     ]
    }
   ],
   "source": [
    "# 读取numpy文件 https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html#numpy.genfromtxt\n",
    "import numpy\n",
    "world_alcohol = numpy.genfromtxt(\"world_alcohol.txt\", delimiter=\",\", dtype=str)\n",
    "print ('world_alcohol的类型：\\n', type(world_alcohol))\n",
    "print ('world_alcohol的详细信息：\\n', world_alcohol)\n",
    "print ('world_alcohol第2行第2列元素值：\\n', world_alcohol[2,2]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向量\n",
      " [ 5 10 15 20]\n",
      "向量形状\n",
      " (4,)\n",
      "向量前3个元素值\n",
      " [ 5 10 15]\n",
      "元素值判断\n",
      " [False  True False False]\n",
      "取元素值==10的元素 [10]\n",
      "取既等于10又等于5的元素 []\n",
      "取等于或等于5的元素 [ 5 10]\n",
      "对等于10或等于5的元素赋值50 [50 50 15 20]\n",
      "矩阵\n",
      " [[ 5 10 15]\n",
      " [20 25 30]\n",
      " [35 40 45]]\n",
      "矩阵形状\n",
      " (3, 3)\n",
      "矩阵第1列元素值\n",
      " [10 25 40]\n",
      "矩阵前2列元素值\n",
      " [[ 5 10]\n",
      " [20 25]\n",
      " [35 40]]\n",
      "矩阵第1,2行，前2列元素\n",
      " [[20 25]\n",
      " [35 40]]\n",
      "元素值判断\n",
      " [[False False False]\n",
      " [ True False False]\n",
      " [False False False]]\n",
      "取第1列中元素值==25的元素所在行 [[20 25 30]]\n"
     ]
    }
   ],
   "source": [
    "# 向量\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "print ('向量\\n', vector)\n",
    "print ('向量形状\\n', vector.shape)\n",
    "print ('向量前3个元素值\\n', vector[:3])\n",
    "print ('元素值判断\\n', vector==10)\n",
    "print ('取元素值==10的元素', vector[vector==10])\n",
    "print ('取既等于10又等于5的元素', vector[(vector==10) & (vector==5)])\n",
    "print ('取等于或等于5的元素', vector[(vector==10) | (vector == 5)])\n",
    "vector[(vector==10) | (vector==5)] = 50\n",
    "print ('对等于10或等于5的元素赋值50',  vector)\n",
    "# 矩阵\n",
    "matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])\n",
    "print ('矩阵\\n', matrix)\n",
    "print ('矩阵形状\\n', matrix.shape)\n",
    "print ('矩阵第1列元素值\\n', matrix[:,1])\n",
    "print ('矩阵前2列元素值\\n', matrix[:,0:2])\n",
    "print ('矩阵第1,2行，前2列元素\\n', matrix[1:3, 0:2])\n",
    "print ('元素值判断\\n', matrix == 20)\n",
    "print ('取第1列中元素值==25的元素所在行', matrix[matrix[:,1]==25,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<U1\n",
      "['1' '2' '3']\n",
      "float64\n",
      "[1. 2. 3.]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵元素类型转换\n",
    "vector = numpy.array([\"1\", \"2\", \"3\"])\n",
    "print (vector.dtype)\n",
    "print (vector)\n",
    "vector = vector.astype(float)\n",
    "print (vector.dtype)\n",
    "print (vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向量求极值\n",
    "vector = numpy.array([5, 10, 15, 20])\n",
    "vector.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵按行求和： [ 30  75 120]\n",
      "矩阵按列求和： [60 75 90]\n"
     ]
    }
   ],
   "source": [
    "matrix = numpy.array([[5, 10, 15],\n",
    "                      [20, 25, 30],\n",
    "                      [35, 40, 45]])\n",
    "# 矩阵按行求和\n",
    "print ('矩阵按行求和：', matrix.sum(axis=1))\n",
    "# 矩阵按列求和\n",
    "print ('矩阵按列求和：', matrix.sum(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arange函数生成向量a： [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]\n",
      "a的类型： <class 'numpy.ndarray'>\n",
      "a元素的类型： int32\n",
      "a的维度： 1\n",
      "a的形状： (15,)\n",
      "a中元素个数： 15\n",
      "a变成3*5矩阵b：\n",
      " [[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n",
      "b的类型： <class 'numpy.ndarray'>\n",
      "b元素的类型： int32\n",
      "b的维度： 2\n",
      "b的形状： (3, 5)\n",
      "b中元素个数： 15\n",
      "arange函数生成向量c： [10 15 20 25]\n",
      "arange函数生成向量d： [0.  0.3 0.6 0.9 1.2 1.5 1.8]\n",
      "linspace函数生成指定区间和个数的向量e： [0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259\n",
      " 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317\n",
      " 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081\n",
      " 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992\n",
      " 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903\n",
      " 1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814\n",
      " 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725\n",
      " 2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636\n",
      " 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547\n",
      " 3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458\n",
      " 3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369\n",
      " 4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228\n",
      " 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191\n",
      " 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102\n",
      " 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012\n",
      " 5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923\n",
      " 6.09278575 6.15625227 6.21971879 6.28318531]\n"
     ]
    }
   ],
   "source": [
    "# arange函数1\n",
    "import numpy as np\n",
    "a = np.arange(15)\n",
    "print ('arange函数生成向量a：', a)\n",
    "print ('a的类型：', type(a))\n",
    "print ('a元素的类型：', a.dtype.name)\n",
    "print ('a的维度：', a.ndim)\n",
    "print ('a的形状：', a.shape)\n",
    "print ('a中元素个数：', a.size)\n",
    "# reshape函数\n",
    "b = a.reshape(3, 5)\n",
    "print ('a变成3*5矩阵b：\\n', b)\n",
    "print ('b的类型：', type(b))\n",
    "print ('b元素的类型：', b.dtype.name)\n",
    "print ('b的维度：', b.ndim)\n",
    "print ('b的形状：', b.shape)\n",
    "print ('b中元素个数：',b.size)\n",
    "# arange函数2\n",
    "print ('arange函数生成向量c：', np.arange(10, 30, 5))\n",
    "print ('arange函数生成向量d：', np.arange(0, 2, 0.3))\n",
    "# linspace函数\n",
    "from numpy import pi\n",
    "print ('linspace函数生成指定区间和个数的向量e：', np.linspace(0, 2*pi, 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全0矩阵：\n",
      " [[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "全1矩阵：\n",
      " [[[1 1 1 1]\n",
      "  [1 1 1 1]\n",
      "  [1 1 1 1]]\n",
      "\n",
      " [[1 1 1 1]\n",
      "  [1 1 1 1]\n",
      "  [1 1 1 1]]]\n"
     ]
    }
   ],
   "source": [
    "# 全0矩阵\n",
    "print ('全0矩阵：\\n', np.zeros((3,4)))\n",
    "# 全1矩阵\n",
    "print ('全1矩阵：\\n', np.ones((2,3,4), dtype=np.int32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机初始化矩阵：\n",
      " [[0.42916014 0.75200512 0.80929323]\n",
      " [0.65669041 0.32626574 0.19658161]]\n"
     ]
    }
   ],
   "source": [
    "# 随机初始化矩阵 https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html#numpy.random.random\n",
    "print ('随机初始化矩阵：\\n', np.random.random((2, 3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= [20 30 40 50]\n",
      "b= [0 1 2 3]\n",
      "c=a-b\n",
      " [20 29 38 47]\n",
      "a*2\n",
      " [ 40  60  80 100]\n",
      "c-1\n",
      " [19 28 37 46]\n",
      "b**2\n",
      " [0 1 4 9]\n",
      "a<35\n",
      " [ True  True False False]\n"
     ]
    }
   ],
   "source": [
    "# 向量加减乘方\n",
    "a = np.array([20, 30, 40, 50])\n",
    "b = np.arange(4)\n",
    "print ('a=', a)\n",
    "print ('b=', b)\n",
    "c = a-b\n",
    "print ('c=a-b\\n', a-b)\n",
    "print ('a*2\\n', a*2)\n",
    "print ('c-1\\n', c-1)\n",
    "print ('b**2\\n', b**2)\n",
    "print ('a<35\\n', a<35)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵A\n",
      " [[1 1]\n",
      " [0 1]]\n",
      "矩阵B\n",
      " [[2 0]\n",
      " [3 4]]\n",
      "A与B的点积\n",
      " [[2 0]\n",
      " [0 4]]\n",
      "矩阵乘法\n",
      " [[5 4]\n",
      " [3 4]]\n",
      "矩阵乘法\n",
      " [[5 4]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵乘法和内积\n",
    "A = np.array([[1, 1],\n",
    "              [0, 1]])\n",
    "B = np.array([[2, 0],\n",
    "              [3, 4]])\n",
    "print ('矩阵A\\n', A)\n",
    "print ('矩阵B\\n', B)\n",
    "print ('A与B的点积\\n', A*B)\n",
    "print ('矩阵乘法\\n', A.dot(B))\n",
    "print ('矩阵乘法\\n', np.dot(A, B))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B=\n",
      " [0 1 2]\n",
      "B中每个元素求指数：\n",
      " [1.         2.71828183 7.3890561 ]\n",
      "B中每个元素求平方根：\n",
      " [0.         1.         1.41421356]\n"
     ]
    }
   ],
   "source": [
    "# 向量中每个元素求指数\n",
    "import numpy as np\n",
    "B = np.arange(3)\n",
    "print ('B=\\n', B)\n",
    "print ('B中每个元素求指数：\\n', np.exp(B))\n",
    "print ('B中每个元素求平方根：\\n', np.sqrt(B))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      " [[8. 6. 6. 6.]\n",
      " [1. 1. 4. 4.]\n",
      " [8. 5. 5. 4.]]\n",
      "A转成向量：\n",
      " [8. 6. 6. 6. 1. 1. 4. 4. 8. 5. 5. 4.]\n",
      "对矩阵A重新变换形状：\n",
      " [[8. 6.]\n",
      " [6. 6.]\n",
      " [1. 1.]\n",
      " [4. 4.]\n",
      " [8. 5.]\n",
      " [5. 4.]]\n",
      "矩阵A的转置：\n",
      " [[8. 6. 1. 4. 8. 5.]\n",
      " [6. 6. 1. 4. 5. 4.]]\n",
      "矩阵A重新变换形状2：\n",
      " [[8. 6. 6. 6.]\n",
      " [1. 1. 4. 4.]\n",
      " [8. 5. 5. 4.]]\n"
     ]
    }
   ],
   "source": [
    "# 生成3*4的随机矩阵，矩阵元素取值范围为[0,10),对于随机生成的数值扩充10倍后向下取整\n",
    "A = np.floor(10*np.random.random((3,4)))\n",
    "print ('A=\\n', A)\n",
    "# 矩阵转成向量\n",
    "print ('A转成向量：\\n', A.ravel())\n",
    "# reshape的另一种方式\n",
    "A.shape = (6, 2)\n",
    "print ('对矩阵A重新变换形状：\\n', A)\n",
    "# 矩阵的转置\n",
    "print ('矩阵A的转置：\\n', A.T)\n",
    "# reshape自动确定维数\n",
    "A = A.reshape(3, -1)\n",
    "print ('矩阵A重新变换形状2：\\n', A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      " [[3. 4.]\n",
      " [0. 1.]]\n",
      "B=\n",
      " [[7. 0.]\n",
      " [7. 6.]]\n",
      "C=[A, B]=\n",
      " [[3. 4. 7. 0.]\n",
      " [0. 1. 7. 6.]]\n",
      "D=[A\n",
      "             B]=\n",
      " [[3. 4.]\n",
      " [0. 1.]\n",
      " [7. 0.]\n",
      " [7. 6.]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵拼接\n",
    "import numpy as np\n",
    "A = np.floor(10*np.random.random((2, 2)))\n",
    "B = np.floor(10*np.random.random((2, 2)))\n",
    "print ('A=\\n', A)\n",
    "print ('B=\\n', B)\n",
    "print ('C=[A, B]=\\n', np.hstack((A, B)))\n",
    "print (\"\"\"D=[A\n",
    "             B]=\\n\"\"\", np.vstack((A, B)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      " [[7. 3. 0. 2. 2. 7. 6. 3. 4. 7. 7. 7.]\n",
      " [3. 7. 0. 7. 0. 0. 5. 3. 3. 6. 5. 1.]]\n",
      "A按行分解为3个矩阵：\n",
      " [array([[7., 3., 0., 2.],\n",
      "       [3., 7., 0., 7.]]), array([[2., 7., 6., 3.],\n",
      "       [0., 0., 5., 3.]]), array([[4., 7., 7., 7.],\n",
      "       [3., 6., 5., 1.]])]\n",
      "A按行分解指定分解位置：\n",
      " [array([[7., 3., 0.],\n",
      "       [3., 7., 0.]]), array([[2.],\n",
      "       [7.]]), array([[2., 7., 6., 3., 4., 7., 7., 7.],\n",
      "       [0., 0., 5., 3., 3., 6., 5., 1.]])]\n",
      "A的转置：\n",
      " [[7. 3.]\n",
      " [3. 7.]\n",
      " [0. 0.]\n",
      " [2. 7.]\n",
      " [2. 0.]\n",
      " [7. 0.]\n",
      " [6. 5.]\n",
      " [3. 3.]\n",
      " [4. 3.]\n",
      " [7. 6.]\n",
      " [7. 5.]\n",
      " [7. 1.]]\n",
      "A按列分解为3个矩阵：\n",
      " [array([[7., 3.],\n",
      "       [3., 7.],\n",
      "       [0., 0.],\n",
      "       [2., 7.]]), array([[2., 0.],\n",
      "       [7., 0.],\n",
      "       [6., 5.],\n",
      "       [3., 3.]]), array([[4., 3.],\n",
      "       [7., 6.],\n",
      "       [7., 5.],\n",
      "       [7., 1.]])]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵分解\n",
    "A = np.floor(10*np.random.random((2, 12)))\n",
    "print ('A=\\n', A)\n",
    "print ('A按行分解为3个矩阵：\\n', np.hsplit(A, 3))\n",
    "print ('A按行分解指定分解位置：\\n', np.hsplit(A, (3, 4)))\n",
    "A = A.T\n",
    "print ('A的转置：\\n', A)\n",
    "print ('A按列分解为3个矩阵：\\n', np.vsplit(A, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "b=\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "id(a)= 2063907350128\n",
      "id(b)= 2063907350128\n",
      "==========\n",
      "a=\n",
      " [[   0    1    2    3]\n",
      " [1234    5    6    7]\n",
      " [   8    9   10   11]]\n",
      "c=\n",
      " [[   0    1    2    3 1234    5]\n",
      " [   6    7    8    9   10   11]]\n",
      "id(a)= 2063907350128\n",
      "id(c)= 2063907349968\n",
      "==========\n",
      "a=\n",
      " [[   0    1    2    3]\n",
      " [1234    5    6    7]\n",
      " [   8    9   10   11]]\n",
      "d=\n",
      " [[9999    1    2    3]\n",
      " [1234    5    6    7]\n",
      " [   8    9   10   11]]\n",
      "id(a)= 2063907350128\n",
      "id(d)= 2063907511472\n"
     ]
    }
   ],
   "source": [
    "# 向量的多引用\n",
    "a = np.arange(12)\n",
    "b = a\n",
    "b.shape = (3, 4)\n",
    "print ('a=\\n', a)\n",
    "print ('b=\\n', b)\n",
    "print ('id(a)=', id(a))\n",
    "print ('id(b)=', id(b))\n",
    "print ('='*10)\n",
    "# 浅复制 两个变量指向不同的对象，但是两个对象共用同一块内存\n",
    "c = a.view()\n",
    "c.shape = (2, 6)\n",
    "c[0, 4] = 1234\n",
    "print ('a=\\n', a)\n",
    "print ('c=\\n', c)\n",
    "print ('id(a)=', id(a))\n",
    "print ('id(c)=', id(c))\n",
    "print ('='*10)\n",
    "# 深复制 两个变量指向不同的对象，两个对象有各自的内存\n",
    "d = a.copy()\n",
    "d[0, 0] = 9999\n",
    "print ('a=\\n', a)\n",
    "print ('d=\\n', d)\n",
    "print ('id(a)=', id(a))\n",
    "print ('id(d)=', id(d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data= [[ 0.          0.84147098  0.90929743  0.14112001]\n",
      " [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]\n",
      " [ 0.98935825  0.41211849 -0.54402111 -0.99999021]\n",
      " [-0.53657292  0.42016704  0.99060736  0.65028784]\n",
      " [-0.28790332 -0.96139749 -0.75098725  0.14987721]]\n",
      "按列找矩阵每列最大值的位置 [2 0 3 1]\n",
      "按列找矩阵每列最大值 [0.98935825 0.84147098 0.99060736 0.6569866 ]\n",
      "按行找矩阵每行最大值的位置 [2 3 0 2 3]\n"
     ]
    }
   ],
   "source": [
    "# 求最大值位置\n",
    "import numpy as np\n",
    "data = np.sin(np.arange(20).reshape(5,4))\n",
    "print ('data=', data)\n",
    "# 按列找最大值位置\n",
    "print ('按列找矩阵每列最大值的位置', np.argmax(data, 0))\n",
    "# 取出最大值\n",
    "indx = np.argmax(data, 0)\n",
    "print ('按列找矩阵每列最大值', data[indx, np.arange(data.shape[1])])\n",
    "# 按行找最大值位置\n",
    "print ('按行找矩阵每行最大值的位置', np.argmax(data, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= [10 20 30]\n",
      "B=\n",
      " [[10 20 30 10 20 30 10 20 30]\n",
      " [10 20 30 10 20 30 10 20 30]]\n"
     ]
    }
   ],
   "source": [
    "# 将向量作为元素，扩充成矩阵\n",
    "a = np.arange(10, 40, 10)\n",
    "print ('a=', a)\n",
    "B = np.tile(a, (2, 3)) # 行是原来的2倍， 列是原来的3倍\n",
    "print ('B=\\n', B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      " [[4 3 5]\n",
      " [1 2 1]]\n",
      "A按行排序\n",
      " [[3 4 5]\n",
      " [1 1 2]]\n",
      "a= [4 3 1 2]\n",
      "a按行排序\n",
      " [1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "# 排序\n",
    "A = np.array([[4, 3, 5], \n",
    "              [1, 2, 1]])\n",
    "print ('A=\\n', A)\n",
    "# 按行排序\n",
    "B = np.sort(A, axis=1)\n",
    "print ('A按行排序\\n', B)\n",
    "# 向量按行排序方法2\n",
    "a = np.array([4, 3, 1, 2])\n",
    "print ('a=', a)\n",
    "sort_indx = np.argsort(a)\n",
    "print ('a按行排序\\n', a[sort_indx])"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
